﻿namespace Wuqi.Webdiyer
{
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Windows.Forms;

    public class AspNetPagerActionList : DesignerActionList
    {
        private AspNetPager pager;
        private DesignerActionUIService svc;

        public AspNetPagerActionList(IComponent component) : base(component)
        {
            this.pager = component as AspNetPager;
            this.svc = base.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            items.Add(new DesignerActionHeaderItem("分页方式"));
            items.Add(new DesignerActionPropertyItem("UrlPaging", "启用Url分页", "分页"));
            items.Add(new DesignerActionHeaderItem("外观及文本"));
            items.Add(new DesignerActionPropertyItem("ShowFirstLast", "显示首页和尾页按钮", "外观"));
            items.Add(new DesignerActionPropertyItem("ShowPrevNext", "显示上一页和下一页按钮", "外观"));
            items.Add(new DesignerActionPropertyItem("CenterCurrentPageButton", "居中显示当前页数字索引按钮", "外观"));
            items.Add(new DesignerActionMethodItem(this, "SetPageIndexBox", "页索引文本或下拉框...", "外观", true));
            items.Add(new DesignerActionMethodItem(this, "SetNavText", "导航按钮显示文本...", "外观", true));
            items.Add(new DesignerActionMethodItem(this, "SetCustomInfoHTML", "自定义信息区显示方式及内容...", "外观", true));
            items.Add(new DesignerActionMethodItem(this, "ShowSPWindow", "分页存储过程生成工具...", "工具及帮助", true));
            items.Add(new DesignerActionMethodItem(this, "ShowAboutForm", "关于AspNetPager...", "工具及帮助", true));
            return items;
        }

        public void SetCustomInfoHTML()
        {
            CustomInfoForm form = new CustomInfoForm(this.pager.ShowCustomInfoSection, this.pager.CustomInfoHTML);
            if (form.ShowDialog() == DialogResult.OK)
            {
                this.SetProperty("ShowCustomInfoSection", form.ShowCustomSection);
                this.SetProperty("CustomInfoHTML", form.CustomInfoHtml);
            }
        }

        public void SetNavText()
        {
            NavTextForm form = new NavTextForm(this.pager.FirstPageText, this.pager.LastPageText, this.pager.PrevPageText, this.pager.NextPageText);
            if (form.ShowDialog() == DialogResult.OK)
            {
                this.SetProperty("FirstPageText", form.FirstPageText);
                this.SetProperty("LastPageText", form.LastPageText);
                this.SetProperty("PrevPageText", form.PrevPageText);
                this.SetProperty("NextPageText", form.NextPageText);
            }
        }

        public void SetPageIndexBox()
        {
            PageIndexBoxForm form = new PageIndexBoxForm(this.pager.ShowPageIndexBox, this.pager.ShowBoxThreshold, this.pager.PageIndexBoxType, this.pager.TextBeforePageIndexBox, this.pager.TextAfterPageIndexBox, this.pager.SubmitButtonText);
            if (form.ShowDialog() == DialogResult.OK)
            {
                this.SetProperty("ShowPageIndexBox", form.ShowIndexBox);
                this.SetProperty("ShowBoxThreshold", form.Threshold);
                this.SetProperty("PageIndexBoxType", form.BoxType);
                this.SetProperty("TextBeforePageIndexBox", form.TextBeforeBox);
                this.SetProperty("TextAfterPageIndexBox", form.TextAfterBox);
                this.SetProperty("SubmitButtonText", form.SubmitButtonText);
            }
        }

        private void SetProperty(string propertyName, object value)
        {
            TypeDescriptor.GetProperties(this.pager)[propertyName].SetValue(this.pager, value);
        }

        public void ShowSPWindow()
        {
            new StoredProcForm().ShowDialog();
        }

        public bool CenterCurrentPageButton
        {
            get
            {
                return this.pager.CenterCurrentPageButton;
            }
            set
            {
                this.SetProperty("CenterCurrentPageButton", value);
            }
        }

        public bool ShowFirstLast
        {
            get
            {
                return this.pager.ShowFirstLast;
            }
            set
            {
                this.SetProperty("ShowFirstLast", value);
            }
        }

        public bool ShowPrevNext
        {
            get
            {
                return this.pager.ShowPrevNext;
            }
            set
            {
                this.SetProperty("ShowPrevNext", value);
            }
        }

        public bool UrlPaging
        {
            get
            {
                return this.pager.UrlPaging;
            }
            set
            {
                this.SetProperty("UrlPaging", value);
            }
        }
    }
}

